<template>
  <div id="table_container">
    <el-card style="margin-bottom: 5px; text-align: left">
      <el-input v-model="keyWord" placeholder="输入搜索内容" style="width: 200px;" @keyup.enter.native="handleFilter"></el-input>
      <el-select v-model="status" placeholder="状态" style="width: 90px;margin-left: 10px;">
        <el-option v-for="item in importanceOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="handleFilter()" style="margin-left: 10px;">
        查询
      </el-button>
      <el-button v-permission="['user:add']" class="create_btn" style="float: right;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </el-card>
    <el-card>
      <el-table
          size="small"
          :data="tableData"
          v-loading="loading"
          row-key="id"
          border>
        <el-table-column
            type="index"
            label="序号"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="username"
            label="账号">
        </el-table-column>
        <el-table-column
            prop="name"
            label="姓名">
        </el-table-column>
        <el-table-column
            show-overflow-tooltip
            prop="dept_info.name"
            label="部门">
        </el-table-column>
        <el-table-column
            prop="phone"
            align="center"
            label="联系电话"
            width="110">
        </el-table-column>
        <el-table-column
            prop="email"
            label="邮箱">
        </el-table-column>
        <el-table-column
            prop="roles"
            show-overflow-tooltip
            label="角色">
          <template slot-scope="scope">
            <el-tag type='primary' disable-transitions v-for="item in scope.row.roles_info">
              {{ item.name }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            width="70">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.status === true ? 'success' :'danger' "
                disable-transitions>{{ scope.row.status === true ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <!--      <el-table-column-->
        <!--          label="创建时间"-->
        <!--          align="center"-->
        <!--          width="120">-->
        <!--        <template slot-scope="scope">-->
        <!--          <i class="el-icon-time"></i>-->
        <!--          <span style="margin-left: 10px">{{ scope.row.create_time }}</span>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column
            fixed="right"
            width="180"
            align="center"
            label="操作">
          <template slot-scope="scope">
            <el-button v-permission="['user:edit']" @click="handleEdit(scope.row)" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button v-permission="['user:delete']" @click="handleDelete(scope.row)" type="danger" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="pageTotal>0"
          :total="pageTotal"
          :page.sync="page"
          :pageSize.sync="pageSize"
          @pagination="getDataList"/>
    </el-card>

    <el-dialog title="新建" top="10vh" width="400px" @close="handleClose" :visible.sync="createDialogFormVisible">
      <el-form :model="form" label-width="80px">

        <el-form-item label="姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码">
          <el-input v-model="form.password" autocomplete="off" show-password></el-input>
        </el-form-item>

        <el-form-item label="身份证号">
          <el-input v-model.number="form.id_card" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机">
          <el-input v-model.number="form.phone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input placeholder="请输入内容" v-model="form.email" autocomplete="off">
          </el-input>
        </el-form-item>


        <el-form-item label="角色">
          <el-select filterable v-model="form.roles" multiple placeholder="请选择">
            <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="岗位">
          <el-select filterable v-model="form.posts" multiple placeholder="请选择">
            <el-option
                v-for="item in postOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="部门">
          <el-select filterable v-model="form.dept" placeholder="请选择">
            <el-option
                v-for="item in deptOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCreate">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑" top="10vh" width="400px" @close="handleClose" :visible.sync="editDialogFormVisible">
      <el-form :model="form" label-width="80px">

        <el-form-item label="姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码">
          <el-input v-model="form.password" autocomplete="off" show-password></el-input>
        </el-form-item>

        <el-form-item label="身份证号">
          <el-input v-model.number="form.id_card" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机">
          <el-input v-model.number="form.phone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input placeholder="请输入内容" v-model="form.email" autocomplete="off">
          </el-input>
        </el-form-item>


        <el-form-item label="角色">
          <el-select filterable v-model="form.roles" multiple placeholder="请选择">
            <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="岗位">
          <el-select filterable v-model="form.posts" multiple placeholder="请选择">
            <el-option
                v-for="item in postOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="部门">
          <el-select filterable v-model="form.dept" placeholder="请选择">
            <el-option
                v-for="item in deptOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="查看信息" @close="handleClose" :visible.sync="showDialogFormVisible">
      <el-form :model="form" disabled>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码">
              <el-input v-model="form.password" autocomplete="off" show-password></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="身份证号">
              <el-input v-model="form.id_card" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机">
              <el-input v-model="form.phone" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="邮箱">
              <el-input placeholder="请输入内容" v-model="form.email" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item></el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="角色">
              <el-select v-model="form.roles" multiple placeholder="请选择" filterable style="width: 100%">
                <el-option
                    v-for="item in roleOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="岗位">
              <el-select v-model="form.posts" multiple placeholder="请选择" filterable style="width: 100%">
                <el-option
                    v-for="item in postOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="部门">
              <el-cascader
                  placeholder="请选择"
                  v-model="form.dept"
                  :options="deptOptions"
                  :props="{
                    label:'name',
                    value:'id'
                  }">
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-radio-group v-model="form.gender">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="2">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer"></div>
    </el-dialog>
  </div>
</template>

<script>
import {createUser, deleteUser, getDeptOptions, getPostOptions, getRoleOptions, getUserList, login, updateUser} from "@/api";
import pagination from '@/components/Pagenation'

export default {
  name: "index",
  components: {
    pagination
  },
  data() {
    return {
      loading: true,
      tableData: [],
      createDialogFormVisible: false,
      showDialogFormVisible: false,
      editDialogFormVisible: false,
      form: {
        name: undefined,
        password: undefined,
        id_card: undefined,
        phone: undefined,
        email: undefined,
        gender: 0,
        roles: undefined,
        posts: undefined,
        dept: undefined,
      },
      formLabelWidth: '80px',
      keyWord: undefined,
      importanceOptions: [{label: '启用', value: 1}, {label: '停用', value: 0}],
      status: undefined,
      roleOptions: [],
      postOptions: [],
      deptOptions: [],

      // 分页器相关
      page: 1,
      pageTotal: 0,
      pageSize: 10,

    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getDataList(params = {}) {
      this.loading = true
      params['page'] = this.page
      params['pageSize'] = this.pageSize
      params['status'] = this.status
      params['keyWord'] = this.keyWord
      getUserList(params).then(res => {
        this.tableData = res.data.results
        this.pageTotal = res.data.count
        setTimeout(() => {
          this.loading = false
        }, 1.5 * 500)
      })
    },
    handleClose() {
      this.form = {
        name: undefined,
        password: undefined,
        id_card: undefined,
        phone: undefined,
        email: undefined,
        gender: 0,
        roles: undefined,
        posts: undefined,
        dept: undefined,
      }

    },
    handleDelete(row) {
      deleteUser(row.id).then(res => {
        this.$message.success('删除成功！')
        this.getDataList()
      }).catch(err => {
        this.$message.error('删除失败！')
      })
    },
    handleFilter() {
      this.getDataList()
    },
    handleCreate() {
      this.createDialogFormVisible = true
      this.loadDialogOptions()
    },
    handleDetail(row) {
      this.showDialogFormVisible = true
      this.loadDialogOptions()
      this.form = {...row}
    },
    handleEdit(row) {
      this.editDialogFormVisible = true
      this.loadDialogOptions()
      this.form = {...row}
    },
    submitCreate() {
      let data = this.form
      createUser(data).then(res => {
        this.$message.success('新建成功！')
        this.createDialogFormVisible = false
        this.getDataList()
        // this.$router.go(0)
      })
    },
    submitUpdate() {
      let data = this.form
      updateUser(data).then(res => {
        this.$message.success('修改成功！')
        this.editDialogFormVisible = false
        this.getDataList()
        // this.$router.go(0)
      })
    },
    loadDialogOptions() {
      getRoleOptions().then(res => {
        this.roleOptions = res.data
      });
      getPostOptions().then(res => {
        this.postOptions = res.data
      });
      getDeptOptions().then(res => {
        this.deptOptions = res.data
      });
    },

  },

}
</script>

<style scoped>

</style>